using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Deepcode.Flux.Core.Services.FluxClient;

namespace Deepcode.Flux.Core.UI.Controls
{
	/// <summary>
	/// Section is a gradient strip with a title (exactly the same as a collapsesection,
	/// but this cannot be collapsed or expanded - it's always expanded)
	/// </summary>
	[
	ParseChildren(false),
	PersistChildren(true)
	]
	public class Section : Deepcode.Flux.Core.UI.Controls.Base.BaseControl
	{
		#region Control Properties
		/// <summary>
		/// Specifies the header text for a heading / subheading panel
		/// </summary>
		[Category("Display"), Description("Specified the header text for the collapsable section")]
		public string Title
		{
			get
			{
				string s = (string) ViewState["Txt"];
				if( s == null ) return String.Empty;
				return s;
			}
			set
			{
				ViewState["Txt"] = value;
			}
		}
		/// <summary>
		/// Specifies whether or not the section should include the footer bar or not. (true by default)
		/// </summary>
		public bool WithFooter
		{
			get
			{
				object o = ViewState["foot"];
				if( o == null ) return true;
				return (bool) o;
			}
			set
			{
				ViewState["foot"] = value;
			}
		}
		#endregion
		#region Rendering
		/// <summary>
		/// Renders the HTML for the tag opening
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
		{
			if (Title != "")
			{
				string BackImage = ClientHelper.GetFluxClientFile("Section/Img/panelTile.jpg");
				string ControlDivID = this.ClientID + "_Area";

				string IMGTAG = String.Format("<img src='{0}' align='absmiddle'/>",
					Page.ResolveUrl(BackImage));

				writer.Write("<DIV CLASS='SEC_TITLEAREANOCLICK'>");
				writer.Write("<table width='100%' cellspacing=0 cellpadding=0 border=0>");
				writer.Write("<tr>");
				writer.Write("<td width='100%' background='{1}' class='SEC_TITLEAREA'>{0}</td>", this.Title, Page.ResolveUrl(BackImage));
				writer.Write("<td align=right background='{1}' class='SEC_TITLEAREAIMAGE'>{0}</td>", IMGTAG, Page.ResolveUrl(BackImage));
				writer.Write("</tr></table></DIV>");
				writer.Write("<DIV ID='{0}' NAME='{0}'>", ControlDivID);
			}
			else
			{
				string ControlDivID = this.ClientID + "_Area";
				writer.Write("<DIV ID='{0}' NAME='{0}'>", ControlDivID);
			}
		}
		/// <summary>
		/// Renders the HTML for the closing tag
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
		{
			if( this.WithFooter )
			{
				string FooterImage = ClientHelper.GetFluxClientFile("Section/Img/panelFooterTile.jpg");
				writer.Write("<table width='100%' height='5' cellspacing='0' cellpadding='0' border='0'>");
				writer.Write("<tr><td width='100%' height='5' background='{0}'><img src='{0}' width='10' height='5' alt='' border='0'/></td></tr>", FooterImage);
				writer.Write("</table>");
			}
			writer.Write("</DIV>");
		}
		#endregion
		#region Init / Setup
		/// <summary>
		/// Includes the stylesheets needed to render a panel
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			this.LoadClientStyleSheet("Section/Section.css", "Deepcode.Flux.Core.UI.Controls.Section");
		}
		#endregion
	}
}
